package changceilingcolor003;
import javax.management.timer.Timer;

import toxi.geom.Vec3D;
import processing.core.PApplet;

public class Grid {
	ChangCeilingColor003 p5;
	int idX;
	int idY;
	Pt[][] cells;

	Grid(ChangCeilingColor003 _p5, int _idX, int _idY) {
		p5 = _p5;
		idX= _idX;
		idY= _idY;
		cells = new Pt[idX][idY];
	}

	void init() {
		for (int i=0;i<p5.COLS; i++) {
			for (int j=0;j<p5.ROWS; j++) {
				Vec3D v=new Vec3D(i*p5.resulotion, j*p5.resulotion, 0); 
				int colR=0;
				int colG=0;
				int colB=0;
				//int Transparancy=50;
				int Transparancy=40;/////////////////////////////////////////////////////////////////////////////////
				cells[i][j]=new Pt(v,colR,colG,colB,Transparancy,p5);
			}
		}
	}

	void run() {		
			//people leave impact on the whole ground 
			for (int i=0;i<p5.alisPeop.size();i++) {
				Person p=(Person)p5.alisPeop.get(i);
				//	      Vec3D n = pTerr.getLocInGrid(p.pos);
				int ptX = p5.pTerr.xLocInGrid(p.pos);
				int ptY = p5.pTerr.yLocInGrid(p.pos);
                ////////////////////////////////////////////////////////////////////////////////////
				if (cells[ptX][ptY].Transparancy <= 100 && cells[ptX][ptY].Transparancy >= 0 ) {
					cells[ptX][ptY].Transparancy +=10;
				}
				cells[ptX][ptY].StepCount +=1;//for usage
			}
			
			

			//adding the grid point to attraction sets/count the points
			for(int i =0; i<p5.alisDst.size(); ++i)
			{	
				int count=0;
				int countpeople=0;
				int usage=0;
				float density=0.0f;
				
				
				//
				Attraction att =  (Attraction) p5.alisDst.get(i);
				if(att.m_active==true){	
					Vec3D direction = att.GetLocation().copy();
					//find grid points belong to attraction 
					for (int j=0;j<p5.COLS; j++) {
						for (int k=0;k<p5.ROWS; k++) {					
							Vec3D desired = (Vec3D)cells[j][k].loc.sub(att.GetLocation());
							float hDist = desired.magnitude();			
							if(hDist<att.GetAttractionRadious())
							{	
								cells[j][k].colR +=att.GetColorR();
								cells[j][k].colG +=att.GetColorG();
								cells[j][k].colB +=att.GetColorB();
								count++;								
								///////////////usage
								usage+=cells[j][k].StepCount;							
							}
						}
					}
					
					
//count people in one program

					for (int j=0;j<p5.alisPeop.size();j++) {

						Person p=(Person)p5.alisPeop.get(j);
						Vec3D desired = (Vec3D)p.pos.sub(att.GetLocation());
						float hDist = desired.magnitude();			
						if(hDist<att.GetAttractionRadious()){
					    countpeople++;	
					    direction.addSelf(p.pos);
					    
						}
						
					}
															
					if(countpeople!=0){
						direction.scaleSelf((float)1/countpeople);
						//direction.limit((float)att.GetAttractionRadious());
						//direction.addSelf(att.GetLocation());   

						
						
						p5.stroke(0,255,200);
						p5.strokeWeight(5);
						p5.point(direction.x, direction.y, direction.z);
						p5.stroke(0,255,200);
						p5.strokeWeight(2);
						p5.line(att.GetLocation().x,att.GetLocation().y,att.GetLocation().z,direction.x, direction.y, direction.z);
						
					}	
					
					att.output_area=count;
					att.output_countpeople=countpeople;
					////////////////////duration
					if(countpeople!=0){
						att.countTime++;
						att.timer=1;
					}
					
					else {
						if(att.timer==1)att.timerfixed=att.countTime;
						att.timer=0;
						att.countTime=0;
					}
					
					att.output_duration=att.timerfixed;
					
					////////////////density
					if(count!=0)density=countpeople*1000/count;
	
					p5.stroke(255,255,255);
					///////////////////output
					//p5.text("area:"+count+"\n"+"people:"+countpeople+"\n"+"usage:"+(int)usage/100+"\n"+"density:"+density+"\n"+"duration:"+att.countTime+"="+att.output_duration,att.GetLocation().x,att.GetLocation().y);
					//p5.text("area:"+count+"\n"+"people:"+countpeople+"\n"+att.GetLocation().x+"\n"+att.GetLocation().y,att.GetLocation().x,att.GetLocation().y);	
				}


				
			}
			
			
			for (int j=0;j<p5.COLS; j++) {
				for (int k=0;k<p5.ROWS; k++) {					
					cells[j][k].run();
				}
			}			
		}
				



	void clear() {
		for (int i=0;i<p5.COLS; i++) {
			for (int j=0;j<p5.ROWS; j++) {			
				cells[i][j].colR=0;
				cells[i][j].colG=0;
				cells[i][j].colB=0;
				//cells[i][j].Switch=false;

			}
		}
	}

}
